feat(mikro-orm): introduce transaction hooks #2378
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Information
Usage example
The transaction hooks allow you to customize the default transaction behavior. These hooks enable you to execute custom code before and after committing data to the database. These transaction hooks provide a flexible way to extend the default transaction behavior and implement advanced patterns such as the Inbox pattern or domain event dispatching.
BeforeTransactionCommit Hook
The
BeforeTransactionCommit
interface allows you to define hooks that are executed right before committing data to the database. This hook provides a way to modify data within the same transaction context and perform additional operations before the transaction is committed.To use the
BeforeTransactionCommit
hook, first, you have to implement theBeforeTransactionCommit
interface:Then just write your code inside the
$beforeTransactionCommit
method. This code will be executed before the transaction is committed.AfterTransactionCommit Hook
The
AfterTransactionCommit
interface allows you to define hooks that are executed right after committing data to the database. This hook enables you to execute code after the data is committed, making multiple transactions.To use the
AfterTransactionCommit
hook, you have to implement theAfterTransactionCommit
interface:It's important to note that when using the
AfterTransactionCommit
hook, you need to handle eventual consistency and compensatory actions in case of failures on your own.Todos
closes #2370